package com.century.sjt.activity.setting;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.activity.MainActivity;
import com.century.sjt.activity.login.ChooseLanguageActivity;
import com.century.sjt.entity.AboutSJT;
import com.century.sjt.util.Constant;
import com.century.sjt.util.FileUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import java.io.File;

/**
 * @author:周玲波
 * @Description: 设置
 * @date : 2016/8/18 20:28
 */
public class SettingActivity extends BaseActivity {
    private LinearLayout Set_Mima,Clean_hc;
    private LinearLayout Set_Chat;
    private Button Btn_Fh;
    private SjtTitleBar titleBar;
    private TextView tvDeleteCache;
    private LinearLayout set_Language;
    private AboutSJT mAboutSJT= new AboutSJT();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_setting_activity);
        initView();
    }

    @Override
    protected void initView() {
        Set_Mima=(LinearLayout) findViewById(R.id.set_ma);
        Clean_hc=(LinearLayout) findViewById(R.id.clean_hc);
        Set_Chat = (LinearLayout) findViewById(R.id.setting_chat);
        Btn_Fh=(Button) findViewById(R.id.btn_set_retun);
        tvDeleteCache = (TextView) findViewById(R.id.tv_delete_cache);
        titleBar = (SjtTitleBar) findViewById(R.id.settint_title);
        set_Language = (LinearLayout) findViewById(R.id.setting_language);

        titleBar.setLeftIconResource(getResources().getString(R.string.icon_return));
        titleBar.setTitle(getResources().getString(R.string.tab_setting));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        Set_Mima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(SettingActivity.this, Set_pwdActivity.class);
                startActivity(intent);
            }
        });
        Set_Chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //聊天设置
                Intent intent = new Intent();
                intent.setClass(SettingActivity.this, ChatSttingActivity.class);
                startActivity(intent);
            }
        });

        Clean_hc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //询问是否删除
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setMessage(getResources().getString(R.string.text_is_wipe_cache));
                builder.setPositiveButton(getResources().getString(R.string.btn_yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        doCache();
                        getCache();
                        dialog.dismiss();

                    }
                });
                builder.setNegativeButton(getResources().getString(R.string.btn_no), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setCancelable(false);
                builder.create().show();
            }
        });

        SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        String mobile = mainSharedPreferences.getString("mobile", "");
        if(mobile.length()>0) {
            Btn_Fh.setText(getResources().getString(R.string.btn_log_off) + "(" + mobile.substring(0, 3) + " **** " + mobile.substring(mobile.length() - 4, mobile.length()) + ")");
        }
        Btn_Fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = mainSharedPreferences.edit();
                editor.clear();
                editor.putString("isLogin", "0");
                editor.commit();
                Intent intent1 = new Intent();
                intent1.setClass(SettingActivity.this, MainActivity.class);
                startActivity(intent1);
                finish();

            }
        });

        set_Language.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(SettingActivity.this, ChooseLanguageActivity.class);
                intent.putExtra("type","setting");
                startActivityForResult(intent,Constant.REQUESTCODE_LANGUAGE);
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==RESULT_OK){
            switch (requestCode) { //resultCode为回传的标记，我在B中回传的是RESULT_OK
                case Constant.REQUESTCODE_COUNTRY:
//                    Bundle bundle = data.getExtras();
//                    String str = bundle.getString("countryName");//str即为回传的值
                    break;
                case Constant.REQUESTCODE_LANGUAGE:
                    break;
                default:
                    break;
            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }
    private void doCache(){
        Boolean deleteThisPath = false;
//        删除缓存文件
        FileUtil.deleteFolderFile(Constant.SYSTEM_CACHE_PATH, deleteThisPath);
        //判断文件是否存在然后新建
        FileUtil.createImageDir();
    }

    private void getCache(){
        File file=new File(Constant.SYSTEM_CACHE_PATH);
        String szbng = "";
        try {
            //获取缓存文件大小
            long lzbng = FileUtil.getFolderSize(file);
            //格式化文件大小
            szbng = FileUtil.getFormatSize(lzbng);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvDeleteCache.setText(szbng);

    }
    @Override
    protected void onResume() {
        super.onResume();
        getCache();

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }
}
